package network.protocol;

import java.io.Serializable;

/**
 * 
 * @author Erik
 * contains all things worth to know about a player
 */
@SuppressWarnings("serial")
public class PlayerPackage implements Serializable {
	public static final int NEUTRAL_PLAYER = -1;
	private static final double MAX_MORALE = 5.999;
	private static final double STD_SPEED = 1.5;
	private static final double MORALE_SPEED_QUOTA = 4d;
	
	private String nickName;
	private double morale;
	private float speed;
	
	private int supportedPopulation;
	private int totalPop;
	private int totalArmorys;
	private int id;
	
	public PlayerPackage(int id, String nickname){
		this(id, nickname, 0, 1, 0, 0);
	}

	public PlayerPackage(int id, String nickName, double morale, double speed, int totalPop, int totalArmory) {
		this.id = id;
		this.nickName = nickName;
		this.morale = morale;
		this.speed = (float)speed;
		this.totalPop = totalPop;
		this.totalArmorys = totalArmory;
	}


	/**
	 * convert a correct formated string to a PlayerPackage
	 * format: id(int) + \n + nickName(String) + \n + morale(double) + \n + speed(double) 
	 * + \n + totalPop(int) + \n + totalArmorys(int) + \n
	 * @param data
	 */
	public PlayerPackage(String data) {
		int start = 0, end = 0;

		end = data.indexOf('\n');
		id = Integer.parseInt(data.substring(start, end));

		start = end + 1;
		end = data.indexOf('\n', start);
		nickName = data.substring(start, end);

		start = end + 1;
		end = data.indexOf('\n', start);
		morale = Double.parseDouble(data.substring(start, end));

		start = end + 1;
		end = data.indexOf('\n', start);
		speed = Float.parseFloat(data.substring(start, end));

		start = end + 1;
		end = data.indexOf('\n', start);
		totalPop = Integer.parseInt(data.substring(start, end));
		
		start = end + 1;
		end = data.indexOf('\n', start);
		totalArmorys = Integer.parseInt(data.substring(start, end));
	}

	public int getTotalArmorys() {
		return totalArmorys;
	}
	
	public void setTotalArmorys(int totalArmorys){
		this.totalArmorys = totalArmorys;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public int getID() {
		return id;
	}

	public double getMorale() {
		return morale;
	}

	/**
	 * sets the player-morale. 0 <= morale < 6
	 * @param morale
	 */
	public void setMorale(double morale) {
		if (morale >= 0 && morale < MAX_MORALE)
			this.morale = morale;
		else if (morale >= MAX_MORALE)
			morale = MAX_MORALE;
		else
			this.morale = 0;
		speed = (float)((this.morale)/(MORALE_SPEED_QUOTA) + STD_SPEED);
	}

	public float getSpeed() {
		return speed;
	}

	public void setTotalPop(int totalPop) {
		this.totalPop = totalPop;
	}

	public int getTotalPop() {
		return totalPop;
	}

	public boolean equals(PlayerPackage p) {
		return id == p.getID();
	}
	
	/**
	 * reduces the morale by o.25d
	 */
	public void reduceMoraleByTime() {		
		setMorale(morale - 0.25d);
	}

	/**
	 * convert the PlayerPackage to a correct formated string
	 * @return
	 */
	public String toPackage() {
		StringBuffer res = new StringBuffer();
		res.append(id);
		res.append('\n');
		res.append(nickName);
		res.append('\n');
		res.append(morale);
		res.append('\n');
		res.append(speed);
		res.append('\n');
		res.append(totalPop);
		res.append('\n');
		res.append(totalArmorys);
		res.append('\n');
		return res.toString();
	}
	
	public void copy(PlayerPackage p){
		this.morale = p.morale;
		this.id = p.id;
		this.nickName = p.nickName;
		this.speed = p.speed;
		this.totalArmorys = p.totalArmorys;
		this.totalPop = p.totalPop;
	}

	public String toString() {
		return "id: " + id + " nickname: " + nickName + " morale: " + morale + " speed: " + speed + " totalPop: "
				+ totalPop + " totalArmorys: " + totalArmorys;
	}
	
	public int getSupportedPopulation() {
		return supportedPopulation;
	}
	
	public boolean hasGrowth() {
		return totalPop < supportedPopulation;
	}
	
	public void setSupportedPopulation(int suppertSum) {
		supportedPopulation = suppertSum;
		
	}
}
